$OpenBSD: patch-kdecore_network_kresolver_cpp,v 1.11 2007/03/31 23:33:34 espie Exp $
--- kdecore/network/kresolver.cpp.orig	Sat Jul 22 10:16:40 2006
+++ kdecore/network/kresolver.cpp	Sat Mar 31 15:09:22 2007
@@ -66,6 +66,10 @@
 QMutex getXXbyYYmutex;
 #endif
 
+#ifdef __OpenBSD__
+#define USE_OPENBSD 1
+#endif
+
 using namespace KNetwork;
 using namespace KNetwork::Internal;
 
@@ -616,17 +620,28 @@ QStrList KResolver::protocolName(int protonum)
   pe = getprotobynumber(protonum);
 
 #else
+# ifdef USE_OPENBSD // OpenBSD uses an HP/IBM/DEC API
+  struct protoent protobuf;
+  struct protoent_data pdata;
+  ::memset(&pdata, 0, sizeof pdata);
+
+  if (getprotobynumber_r(protonum, &protobuf, &pdata) == 0)
+    pe = &protobuf;
+  else
+    pe = 0;
+
+# else
   size_t buflen = 1024;
   struct protoent protobuf;
   char *buf;
   do
     {
       buf = new char[buflen];
-# ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobynumber_r which returns struct *protoent or NULL
+#  ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobynumber_r which returns struct *protoent or NULL
       if ((pe = getprotobynumber_r(protonum, &protobuf, buf, buflen)) && (errno == ERANGE))
-# else
+#  else
       if (getprotobynumber_r(protonum, &protobuf, buf, buflen, &pe) == ERANGE)
-# endif
+#  endif
 	{
           pe = 0L;
 	  buflen += 1024;
@@ -636,6 +651,7 @@ QStrList KResolver::protocolName(int protonum)
 	break;
     }
   while (pe == 0L);
+# endif
 #endif
 
   // Do common processing
@@ -648,7 +664,9 @@ QStrList KResolver::protocolName(int protonum)
     }
 
 #ifdef HAVE_GETPROTOBYNAME_R
+# ifndef USE_OPENBSD
   delete [] buf;
+# endif
 #endif
 
   return lst;
@@ -663,17 +681,27 @@ QStrList KResolver::protocolName(const char *protoname
   pe = getprotobyname(protoname);
 
 #else
+# ifdef USE_OPENBSD // OpenBSD uses an HP/IBM/DEC API
+  struct protoent protobuf;
+  struct protoent_data pdata;
+  ::memset(&pdata, 0, sizeof pdata);
+
+  if (getprotobyname_r(protoname, &protobuf, &pdata) == 0)
+    pe = &protobuf;
+  else
+    pe = 0;
+# else
   size_t buflen = 1024;
   struct protoent protobuf;
   char *buf;
   do
     {
       buf = new char[buflen];
-# ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobyname_r which returns struct *protoent or NULL
+#  ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobyname_r which returns struct *protoent or NULL
       if ((pe = getprotobyname_r(protoname, &protobuf, buf, buflen)) && (errno == ERANGE))
-# else
+#  else
       if (getprotobyname_r(protoname, &protobuf, buf, buflen, &pe) == ERANGE)
-# endif
+#  endif
 	{
           pe = 0L;
 	  buflen += 1024;
@@ -683,6 +711,7 @@ QStrList KResolver::protocolName(const char *protoname
 	break;
     }
   while (pe == 0L);
+# endif
 #endif
 
   // Do common processing
@@ -695,7 +724,9 @@ QStrList KResolver::protocolName(const char *protoname
     }
 
 #ifdef HAVE_GETPROTOBYNAME_R
+# ifndef USE_OPENBSD
   delete [] buf;
+# endif
 #endif
 
   return lst;
@@ -710,17 +741,28 @@ int KResolver::protocolNumber(const char *protoname)
   pe = getprotobyname(protoname);
 
 #else
+# ifdef USE_OPENBSD // OpenBSD uses an HP/IBM/DEC API
+  struct protoent protobuf;
+  struct protoent_data pdata;
+  ::memset(&pdata, 0, sizeof pdata);
+
+  if (getprotobyname_r(protoname, &protobuf, &pdata) == 0)
+    pe = &protobuf;
+  else
+    pe = 0;
+
+# else
   size_t buflen = 1024;
   struct protoent protobuf;
   char *buf;
   do
     {
       buf = new char[buflen];
-# ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobyname_r which returns struct *protoent or NULL
+#  ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobyname_r which returns struct *protoent or NULL
       if ((pe = getprotobyname_r(protoname, &protobuf, buf, buflen)) && (errno == ERANGE))
-# else
+#  else
       if (getprotobyname_r(protoname, &protobuf, buf, buflen, &pe) == ERANGE)
-# endif
+#  endif
 	{
           pe = 0L;
 	  buflen += 1024;
@@ -730,6 +772,7 @@ int KResolver::protocolNumber(const char *protoname)
 	break;
     }
   while (pe == 0L);
+# endif
 #endif
 
   // Do common processing
@@ -738,7 +781,9 @@ int KResolver::protocolNumber(const char *protoname)
     protonum = pe->p_proto;
 
 #ifdef HAVE_GETPROTOBYNAME_R
+# ifndef USE_OPENBSD
   delete [] buf;
+# endif
 #endif
 
   return protonum;
@@ -753,17 +798,27 @@ int KResolver::servicePort(const char *servname, const
   se = getservbyname(servname, protoname);
 
 #else
+# ifdef USE_OPENBSD // OpenBSD uses an HP/IBM/DEC API
+  struct servent servbuf;
+  struct servent_data sdata;
+  ::memset(&sdata, 0, sizeof sdata);
+  if (getservbyname_r(servname, protoname, &servbuf, &sdata) == 0)
+    se = &servbuf;
+  else
+    se = 0;
+
+# else
   size_t buflen = 1024;
   struct servent servbuf;
   char *buf;
   do
     {
       buf = new char[buflen];
-# ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyname_r which returns struct *servent or NULL
+#  ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyname_r which returns struct *servent or NULL
       if ((se = getservbyname_r(servname, protoname, &servbuf, buf, buflen)) && (errno == ERANGE))
-# else
+#  else
       if (getservbyname_r(servname, protoname, &servbuf, buf, buflen, &se) == ERANGE)
-# endif
+#  endif
 	{
           se = 0L;
 	  buflen += 1024;
@@ -781,7 +836,9 @@ int KResolver::servicePort(const char *servname, const
     servport = ntohs(se->s_port);
 
 #ifdef HAVE_GETSERVBYNAME_R
+# ifndef USE_OPENBSD
   delete [] buf;
+# endif
 #endif
 
   return servport;
@@ -796,17 +853,27 @@ QStrList KResolver::serviceName(const char* servname, 
   se = getservbyname(servname, protoname);
 
 #else
+# ifdef USE_OPENBSD // OpenBSD uses an HP/IBM/DEC API
+  struct servent servbuf;
+  struct servent_data sdata;
+  ::memset(&sdata, 0, sizeof sdata);
+  if (getservbyname_r(servname, protoname, &servbuf, &sdata) == 0)
+    se = &servbuf;
+  else
+    se = 0;
+
+# else
   size_t buflen = 1024;
   struct servent servbuf;
   char *buf;
   do
     {
       buf = new char[buflen];
-# ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyname_r which returns struct *servent or NULL
+#  ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyname_r which returns struct *servent or NULL
       if ((se = getservbyname_r(servname, protoname, &servbuf, buf, buflen)) && (errno == ERANGE))
-# else
+#  else
       if (getservbyname_r(servname, protoname, &servbuf, buf, buflen, &se) == ERANGE)
-# endif
+#  endif
 	{
           se = 0L;
 	  buflen += 1024;
@@ -816,6 +883,7 @@ QStrList KResolver::serviceName(const char* servname, 
 	break;
     }
   while (se == 0L);
+# endif
 #endif
 
   // Do common processing
@@ -828,7 +896,9 @@ QStrList KResolver::serviceName(const char* servname, 
     }
 
 #ifdef HAVE_GETSERVBYNAME_R
+# ifndef USE_OPENBSD
   delete [] buf;
+# endif
 #endif
 
   return lst;
@@ -843,17 +913,27 @@ QStrList KResolver::serviceName(int port, const char *
   se = getservbyport(port, protoname);
 
 #else
+# ifdef USE_OPENBSD // OpenBSD uses an HP/IBM/DEC API
+  struct servent servbuf;
+  struct servent_data sdata;
+  ::memset(&sdata, 0, sizeof sdata);
+  if (getservbyport_r(port, protoname, &servbuf, &sdata) == 0)
+    se = &servbuf;
+  else
+    se = 0;
+
+# else
   size_t buflen = 1024;
   struct servent servbuf;
   char *buf;
   do
     {
       buf = new char[buflen];
-# ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyport_r which returns struct *servent or NULL
+#  ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyport_r which returns struct *servent or NULL
       if ((se = getservbyport_r(port, protoname, &servbuf, buf, buflen)) && (errno == ERANGE))
-# else
+#  else
       if (getservbyport_r(port, protoname, &servbuf, buf, buflen, &se) == ERANGE)
-# endif
+#  endif
 	{
           se = 0L;
 	  buflen += 1024;
@@ -863,6 +943,8 @@ QStrList KResolver::serviceName(int port, const char *
 	break;
     }
   while (se == 0L);
+# endif
+# endif
 #endif
 
   // Do common processing
@@ -875,7 +957,9 @@ QStrList KResolver::serviceName(int port, const char *
     }
 
 #ifdef HAVE_GETSERVBYPORT_R
+# ifndef USE_OPENBSD
   delete [] buf;
+# endif
 #endif
 
   return lst;
